package com.example.javacrawl.util;

import com.example.javacrawl.pojo.LeftGraphData;
import com.example.javacrawl.pojo.RightGraphData;
import com.example.javacrawl.pojo.alibaba.AliData;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class AutoGetDataUtil {




    public static HashMap<String,Object> getData() {

        HashMap<String, Object> hashMap = new HashMap<>();


        for(int i = 0 ; i < 10 ; i ++) {
            try {
                // 创建URL对象
                URL url = new URL("https://jazbasport.en.alibaba.com/core/CommonSupplierTransactionHistoryWidget/chart.action?dmtrack_pageid=675157312102f5691682431103&idx=0&aliMemberEncryptId=IDX1Eg1b390tGIKJnil1tuKLTU8EIfCH9Ny-uZKL-_994q9a_hBMZJoj7QHILRliY17k&_=1682432790436");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                // 获取输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                // 读取JSON数据并存储为字符串
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                String jsonStr = sb.toString();
                System.out.println(jsonStr);

                // 将JSON字符串转换为Java对象
                Gson gson = new Gson();

                //   MyJsonObject obj = gson.fromJson(jsonStr, MyJsonObject.class);


                ObjectMapper objectMapper = new ObjectMapper();
                AliData aliData = objectMapper.readValue(jsonStr, AliData.class);
                // 输出Java对象
                //  System.out.println(obj);
                List<RightGraphData> rightGraphDataList = aliData.getData()
                        .getTradeOrderCntTop()
                        .getValue().stream()
                        .map(t -> {
                            RightGraphData rightGraphData = new RightGraphData();
                            rightGraphData.setOrderCnt(t.getOrderCnt());
                            rightGraphData.setBuyerCountry(t.getBuyerCountry());
                            rightGraphData.setCountryFullName(t.getCountryFullName());
                            rightGraphData.setBeginYearMonth(aliData.getData().getStartYearMonth());
                            rightGraphData.setEndYearMonth(aliData.getData().getEndYearMonth());
                            return rightGraphData;
                        })
                        .collect(Collectors.toList());

                List<LeftGraphData> leftGraphDataList = aliData.getData()
                        .getTradeYearMonth()
                        .getValue()
                        .stream()
                        .filter(t -> {
                            return t.getOrderCnt() != null;
                        }).map(t -> {
                            LeftGraphData leftGraphData = new LeftGraphData();
                            leftGraphData.setOrderCnt(t.getOrderCnt());
                            leftGraphData.setAmt(t.getAmt());
                            leftGraphData.setTradeAmt(t.getTradeAmt());
                            leftGraphData.setYearMonth(t.getYearMonth());
                            return leftGraphData;
                        }).collect(Collectors.toList());

                // 关闭输入流和连接
                br.close();
                conn.disconnect();

                if (hashMap.containsKey("leftGraph")) {
                    List<LeftGraphData> list = (List<LeftGraphData>) hashMap.get("leftGraph");
                    list.addAll(leftGraphDataList);
                } else {
                    hashMap.put("leftGraph", leftGraphDataList);
                }
                if (hashMap.containsKey("rightGraph")) {
                    List<RightGraphData> list = (List<RightGraphData>) hashMap.get("rightGraph");
                    list.addAll(rightGraphDataList);
                } else {
                    hashMap.put("rightGraph", rightGraphDataList);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return hashMap;

    }
}
